/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import java.util.Calendar;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.aergia.vinny.util.DateUtil;
import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class Card implements DatabasePojo {

	private boolean active;
	private String comment;
	private Calendar endDate;
	private int id;
	private String number;
	private Calendar startDate;
	private Volunteer volunteer;

	public Card() {
		// empty
	}

	/**
	 * @param string
	 * @param volunteer2
	 * @param instance
	 */
	public Card(final String number, final Volunteer volunteer, final Calendar startDate) {
		this.number = number;
		this.volunteer = volunteer;
		this.startDate = startDate;
		this.endDate = DateUtil.FAR_IN_FUTURE;
		this.active = true;
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		if (this.getComment() != null) {
			element.setAttribute("comment", this.getComment());
		}
		element.setAttribute("endDate", DateUtil.format(this.getEndDate()));
		element.setAttribute("number", this.getNumber());
		element.setAttribute("startDate", DateUtil.format(this.getStartDate()));
		element.setAttribute("volunteer", Integer.toString(this.getVolunteer().getId()));
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Card)) {
			return false;
		}
		final Card other = (Card) obj;
		if (this.getNumber() == null) {
			if (other.getNumber() != null) {
				return false;
			}
		} else if (!this.getNumber().equals(other.getNumber())) {
			return false;
		}
		return true;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return this.comment;
	}

	/**
	 * @return the endDate
	 */
	public Calendar getEndDate() {
		return this.endDate;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the number
	 */
	public String getNumber() {
		return this.number;
	}

	/**
	 * @return the startDate
	 */
	public Calendar getStartDate() {
		return this.startDate;
	}

	/**
	 * @return the volunteer
	 */
	@ManyToOne(cascade = CascadeType.ALL)
	public Volunteer getVolunteer() {
		return this.volunteer;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.getNumber() == null ? 0 : this.getNumber().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	/**
	 * @param comment
	 *            the comment to set
	 */
	public void setComment(final String comment) {
		this.comment = comment;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(final Calendar endDate) {
		this.endDate = endDate;
	}

	/**
	 * @param card
	 *            the card to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param number
	 *            the number to set
	 */
	public void setNumber(final String number) {
		this.number = number;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(final Calendar startDate) {
		this.startDate = startDate;
	}

	/**
	 * @param volunteer
	 *            the volunteer to set
	 */
	public void setVolunteer(final Volunteer volunteer) {
		this.volunteer = volunteer;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Card " + this.getId() + " [startDate=" + this.getStartDate() + ", endDate=" + this.getEndDate() + ", comment=" + this.getComment() + "]";
	}

}
